package com.ct.ah.lab.controller;

import com.ct.ah.lab.entity.*;
import com.ct.ah.lab.service.ICategoryService;
import com.ct.ah.lab.service.IProductService;
import com.sun.org.glassfish.gmbal.ParameterNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/products")
public class ProductController {
    @Autowired
    private IProductService productService;

    @RequestMapping("/{pid}/get")
    @ResponseBody
    public Map<String,Object> getById(@PathVariable Integer pid){
        Map<String,Object> map = new HashMap<>();
        Product product = productService.getById(pid);
        map.put("state",1000);
        map.put("data",product);
        return map;
    }

    @RequestMapping("/findProductByCid")
    @ResponseBody
    public Map<String,Object> findProductByCid(Integer cid,Integer currentPage, Integer pageSize){
        Map<String,Object> map = new HashMap<>();

        PageRecord pageRecord = productService.listAllByCid(cid, currentPage, pageSize);

        map.put("state",1000);
        map.put("data",pageRecord);
        return map;
    }


    @RequestMapping("/hot")
    @ResponseBody
    public Map<String, Object> hot() {
        Map<String, Object> map = new HashMap<>();

        PageRecord pageRecord = productService.listAllByCid(163, 1, 3);

        map.put("state", 1000);
        map.put("data", pageRecord.getData());

        return map;
    }

    @RequestMapping("/favourite")
    @ResponseBody
    public Map<String, Object> favourite() {
        Map<String, Object> map = new HashMap<>();

        PageRecord pageRecord = productService.listAllByCid(163, 2, 3);

        map.put("state", 1000);
        map.put("data", pageRecord.getData());

        return map;
    }

    @RequestMapping("/findProductByName")
    @ResponseBody
    public Map<String, Object> findProductByName(String name,Integer currentPage, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        System.out.println(name + " " + currentPage + " " + pageSize);
        try {
            System.out.println(URLDecoder.decode(name, "UTF-8"));
            name = URLDecoder.decode(name, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        String[] strArr = name.split("\\s+");
        List<String> keyWordsList = Arrays.asList(strArr);
//        System.out.println(keyWordsList);

        PageRecord pageRecord = productService.listAllByPnames(keyWordsList, currentPage, pageSize);

        map.put("state", 1000);
        map.put("data", pageRecord);

        return map;
    }

    @RequestMapping("/findRandomProduct")
    @ResponseBody
    public Map<String, Object> findRandomProduct() {
        Map<String, Object> map = new HashMap<>();

        List<Product> Products = productService.getRandomProducts();
        map.put("state", 1000);
        map.put("data", Products);

        return map;
    }

    @RequestMapping("/listHot")
    @ResponseBody
    public Map<String, Object> listHot() {
        Map<String, Object> map = new HashMap<>();

        List<Product> Products = productService.getHotProducts();

        map.put("state", 1000);
        map.put("data", Products);

        return map;
    }
}
